/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.persistence.brokersDB;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import systole.domain.analysis.results.averages.AverageByDecade;
import systole.domain.analysis.results.averages.AverageByDecadeFemale;
import systole.domain.analysis.results.averages.AverageByDecadeMale;
import systole.domain.analysis.results.averages.Averages;
import systole.domain.persons.Patient;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.persistence.brokersInterface.AveragesBroker;

/**
 *
 * @author jmj
 */
public class AveragesBrokerDB extends BrokerDB implements AveragesBroker {

    /**
     */
    public AveragesBrokerDB() {
        super();
    }

    @SuppressWarnings("unchecked")
    public Averages getAverages() throws ExceptionDAO {
        try {
            this.logger.logDebug("Getting all averages");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            List<AverageByDecadeFemale> listFemale = currentSession.createCriteria(AverageByDecadeFemale.class).addOrder(Order.asc("decade")).list();
            List<AverageByDecadeMale> listMale = currentSession.createCriteria(AverageByDecadeMale.class).addOrder(Order.asc("decade")).list();
            this.logger.logDebug("get averages successfully");
            Averages averages = new Averages(listFemale, listMale);
            return averages;
        } catch (HibernateException e) {
            this.logger.logError("error on get averages, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los promedios", e.fillInStackTrace());
        }
    }

    public List<AverageByDecade> getAllAverages()throws ExceptionDAO{
     try {
            this.logger.logDebug("Getting all averages");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            List<AverageByDecade> list = currentSession.createCriteria(AverageByDecade.class).addOrder(Order.asc("decade")).list();
            this.logger.logDebug("get averages successfully");
            return list;
        } catch (HibernateException e) {
            this.logger.logError("error on get averages, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los promedios", e.fillInStackTrace());
        }
    }

    public List<AverageByDecadeFemale> getFemaleAverages() throws ExceptionDAO {
        try {
            this.logger.logDebug("Getting all female averages");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            @SuppressWarnings("unchecked")
            List<AverageByDecadeFemale> listFemale = currentSession.createCriteria(AverageByDecadeFemale.class).addOrder(Order.asc("decade")).list();
            this.logger.logDebug("get female averages successfully");
            return listFemale;
        } catch (HibernateException e) {
            this.logger.logError("error on get female averages, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los promedios", e.fillInStackTrace());
        }
    }

    public List<AverageByDecadeMale> getMaleAverages() throws ExceptionDAO {
        try {
            this.logger.logDebug("Getting all male averages");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            @SuppressWarnings("unchecked")
            List<AverageByDecadeMale> listMale = currentSession.createCriteria(AverageByDecadeMale.class).addOrder(Order.asc("decade")).list();
            this.logger.logDebug("get male averages successfully");
            return listMale;
        } catch (HibernateException e) {
            this.logger.logError("error on get male averages, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los promedios", e.fillInStackTrace());
        }
    }

    public List<AverageByDecade> getAveragesByDecade(int decade) throws ExceptionDAO {
        try {
            this.logger.logDebug("Getting all male averages");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            @SuppressWarnings("unchecked")
            List<AverageByDecade> list = currentSession.createCriteria(AverageByDecade.class).add(Restrictions.eq("decade", decade)).list();
            this.logger.logDebug("get male averages successfully");
            return list;
        } catch (HibernateException e) {
            this.logger.logError("error on get male averages, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los promedios", e.fillInStackTrace());
        }
    }

    public AverageByDecadeFemale getFemaleAverageByDecade(int decade) throws ExceptionDAO {
        try {
            this.logger.logDebug("Getting female average for decade " + decade);
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            AverageByDecadeFemale female = (AverageByDecadeFemale) currentSession.createCriteria(AverageByDecadeFemale.class).add(Restrictions.eq("decade", decade)).setMaxResults(1).uniqueResult();
            this.logger.logDebug("get female average successfully");
            return female;
        } catch (HibernateException e) {
            this.logger.logError("error on get female average, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudo obtener el promedio", e.fillInStackTrace());
        }
    }

    public AverageByDecadeMale getMaleAverageByDecade(int decade) throws ExceptionDAO {
        try {
            this.logger.logDebug("Getting male average for decade " + decade);
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            AverageByDecadeMale male = (AverageByDecadeMale) currentSession.createCriteria(AverageByDecadeMale.class).add(Restrictions.eq("decade", decade)).setMaxResults(1).uniqueResult();
            this.logger.logDebug("get male average successfully");
            return male;
        } catch (HibernateException e) {
            this.logger.logError("error on get male average, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudo obtener el promedio", e.fillInStackTrace());
        }
    }

    public AverageByDecade getAverageByPatient(Patient patient) throws ExceptionDAO {
        if ("F".equals(patient.getSex())) {
            return this.getFemaleAverageByDecade(patient.getDecade());
        }
        if ("M".equals(patient.getSex())) {
            return this.getMaleAverageByDecade(patient.getDecade());
        }
        return null;

    }

    public void deleteAllAverages() throws ExceptionDAO {
        try {
            this.logger.logDebug("Deletinga all averages ");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Query query = currentSession.createQuery("delete from AverageByDecade");
            query.executeUpdate();
            this.logger.logDebug("Delete successfully");
        } catch (HibernateException e) {
            this.logger.logError("error on delete averages, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron eliminar los promedios", e.fillInStackTrace());
        }
    }

    public void save(AverageByDecade averageByDecade) throws ExceptionDAO {
        try {
            this.logger.logDebug("Saving average");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.save(averageByDecade);
            this.logger.logDebug("Save successfully");
        } catch (HibernateException e) {
            this.logger.logError("error on save average, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudo guardar los cambios", e.fillInStackTrace());
        }
    }
}
